<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataFormRef"
			@keyup.enter.native="dataFormSubmitHandle()" label-width="120px">

			<el-form-item label="出库单号" prop="orderkey">
				<el-input v-model="dataForm.orderkey" placeholder="出库单号" disabled></el-input>
			</el-form-item>
			<el-form-item label="单据类型" prop="type">
				<!-- <el-input v-model="dataForm.type" placeholder="单据类型"> </el-input> -->
				<el-select v-model="dataForm.type" placeholder="请选择" :disabled="dataForm.status!=0">
					<el-option v-for="item in ordersType" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>

			</el-form-item>
			<el-form-item label="状态" prop="status">
				<!-- <el-input v-model="dataForm.status" placeholder="状态"></el-input> -->
				<el-select v-model="dataForm.status" placeholder="请选择" disabled>
					<el-option v-for="item in ordersStatus" :key="item.list" :label="item.listname"
						:value="reString(item.list)">
					</el-option>
				</el-select>
			</el-form-item>
			
			<el-form-item label="收货人号" prop="cardid">
				<el-input v-model="dataForm.cardId" placeholder="收货人号" :disabled="!!dataForm.id"
					@keyup.enter.stop="getCardMessage" readonly></el-input>
			</el-form-item>
			<el-form-item label="收货人" prop="recipient">
				<el-input v-model="dataForm.recipient" placeholder="收货人" :disabled="!!dataForm.id" readonly ></el-input>
			</el-form-item>
			<el-form-item label="收货人卡ID" prop="recipientId">
				<el-input v-model="dataForm.recipientId" placeholder="收货人卡ID" @keyup.enter.stop="getCardMessage" ></el-input>
			</el-form-item>
			
			
			<el-form-item label="来源单号" prop="sourcekey">
				<el-input v-model="dataForm.sourcekey" placeholder="来源单号"></el-input>
			</el-form-item>
			<el-form-item label="来源单类型" prop="sourcetype">
				<el-input v-model="dataForm.sourcetype" placeholder="来源单类型"></el-input>
			</el-form-item>
			<el-form-item label="来源单时间" prop="sourcedate">
				<el-input v-model="dataForm.sourcedate" placeholder="来源单时间"></el-input>
			</el-form-item>
			<el-form-item label="客户编码" prop="customerkey">
				<el-input v-model="dataForm.customerkey" placeholder="客户编码"></el-input>
			</el-form-item>
			<el-form-item label="客户名称" prop="customername">
				<el-input v-model="dataForm.customername" placeholder="客户名称"></el-input>
			</el-form-item>
			<el-form-item label="联系人" prop="contacts">
				<el-input v-model="dataForm.contacts" placeholder="联系人"></el-input>
			</el-form-item>
			<el-form-item label="电话" prop="phone">
				<el-input v-model="dataForm.phone" placeholder="电话"></el-input>
			</el-form-item>
			<el-form-item label="地址" prop="address">
				<el-input v-model="dataForm.address" placeholder="地址"></el-input>
			</el-form-item>
			<el-form-item label="车牌号" prop="carno">
				<el-input v-model="dataForm.carno" placeholder="车牌号"></el-input>
			</el-form-item>
			<el-form-item label="关联单据" prop="relatedreceiptkey">
				<el-input v-model="dataForm.relatedreceiptkey" placeholder="关联单据"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
			<el-form-item label="回传状态" prop="backstatus">
				<el-input v-model="dataForm.backstatus" placeholder="回传状态" disabled></el-input>
			</el-form-item>
			<el-form-item label="回传时间" prop="backdate">
				<el-input v-model="dataForm.backdate" placeholder="回传时间" disabled></el-input>
			</el-form-item>
			<el-form-item label="回传结果" prop="backresult">
				<el-input v-model="dataForm.backresult" placeholder="回传结果" disabled></el-input>
			</el-form-item>
			<el-form-item label="回传码" prop="backcode">
				<el-input v-model="dataForm.backcode" placeholder="回传码" disabled></el-input>
			</el-form-item>
			<el-form-item label="仓库" prop="warehouse" disabled>
				<el-input v-model="dataForm.warehouse" placeholder="仓库"></el-input>
			</el-form-item>
			<!-- <el-form-item label="创建人" prop="adduser">
            <el-input v-model="dataForm.adduser" placeholder="创建人"></el-input>
          </el-form-item>
          <el-form-item label="创建时间" prop="addtime">
            <el-input v-model="dataForm.addtime" placeholder="创建时间"></el-input>
          </el-form-item>
          <el-form-item label="更新人" prop="updatuser">
            <el-input v-model="dataForm.updatuser" placeholder="更新人"></el-input>
          </el-form-item>
          <el-form-item label="更新时间" prop="updatetime">
            <el-input v-model="dataForm.updatetime" placeholder="更新时间"></el-input>
          </el-form-item> -->
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getOrdersById,
		putOrders,
		postOrders
	} from "@/network/lswms/orders/Orders"
	import {
		ElMessage
	} from 'element-plus'
	import {
		getCodeListByCode
	} from "@/network/lswms/base/Codelist.js"
	import {
		getSequenceByType
	} from "@/network/lswms/base/Sequence.js"
	import {
		getRecipientsinfoByParams
	} from '@/network/lswms/recipient/Recipientsinfo.js'
	import _ from 'lodash'


	export default {
		name: "ordersAddOrUpdate",
		emits: ["refreshDataList"],
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			const dataForm = ref({
				id: '',
				orderkey: '',
				type: '',
				status: 0,
				sourcekey: '',
				sourcetype: '',
				sourcedate: '',
				customerkey: '',
				customername: '',
				contacts: '',
				phone: '',
				address: '',
				carno: '',
				relatedreceiptkey: '',
				remark: '',
				backstatus: '',
				backdate: '',
				backresult: '',
				backcode: '',
				warehouse: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: '',
				cardId:'',
				recipientId:'',
				recipient:''
			});


			const dataRule = reactive({
				orderkey: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				type: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],

			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				nextTick(function() {
					dataFormRef.value.resetFields();

					ordersStatusFn();
					orderTypeFn();
					if (dataForm.value.id) {
						getordersInfo();
					} else {
						orderkeyFn();
					}
				})
			}

			const ordersStatus = ref("");

			const ordersStatusFn = () => {

				getCodeListByCode('OrderStatus').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					ordersStatus.value = res.data;
				}).catch((err) => {});
			}

			const ordersType = ref("");

			const orderTypeFn = () => {

				getCodeListByCode('orderType').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					ordersType.value = res.data;
				}).catch((err) => {});
			}
			const orderkeyFn = () => {

				getSequenceByType('order').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value.orderkey = res.data;
				}).catch((err) => {});
			}



			const getordersInfo = () => {
				getOrdersById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}

			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}

					let qing = () => {
						return !dataForm.value.id ? postOrders(dataForm.value) : putOrders(dataForm
							.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})
			
			//获取领用人信息
			const getCardMessage = () => {
				let data = {
					cardid: dataForm.value.cardId,
					recipientid: dataForm.value.recipientId
				}
				
				getRecipientsinfoByParams(data).then((res) => {
					if (res.code != 0) {
						//dataForm.value.recipientid = '';
						dataForm.value.recipient = '';
						return ElMessage.error(res.msg);
					}
						dataForm.value.cardId = res.data.cardid;
					dataForm.value.recipientId = res.data.recipientid;
					dataForm.value.recipient = res.data.recipient;
				})
			
			}

			return {
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef,
				ordersStatus,
				ordersType,
				getCardMessage
			}
		},
		methods: {
			//hasPermission
			reString: function(val) {
				let n = Number(val)
				if (!isNaN(n)) {
					return n
				} else {
					return val
				}
			}
		}
	}
</script>

<style scoped>

</style>
